-- UIBishopEquip
-- Created by cheny3 Sep/08/2016
-- 装备描述界面

UIBishopEquip = class("UIBishopEquip", function()
    return cc.Layer:create();
end);

function UIBishopEquip.create(skillId, pos, index)
    return UIBishopEquip.new(skillId, pos, index);
end

function UIBishopEquip:ctor(skillId, pos, index)
    -- 初始化
    self:setName("UIBishopEquip");
	-- 重用魔藤技能学习界面
    local node = cc.CSLoader:createNode("layout/bine/BineSkillLearn.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.skillId = skillId;  -- 技能ID
	self.pos = pos;          -- 格子坐标
    self.index = index;

    -- 注册事件处理回调函数
    self:registerEventCallback()

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIBishopEquip:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIBishopEquip:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIBishopEquip 界面析构清理");

            -- 移除所有事件关注
            EventMgr.removeAll("UIBishopEquip");
        end
    end);
end

-- 注册点击事件
function UIBishopEquip:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBishopEquip");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            -- 尝试装备
            self:tryLevelUp();
        end
    end
    btnOk:addTouchEventListener(onOkClick);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(getLocStr("btn_text_equip"));

    -- 死亡情况下才可以不可以点击
    if ME.user.bishop:isDead() then
        btnOk:setTouchEnabled(false);
        btnOk:setBright(false);
    else
        btnOk:setTouchEnabled(true);
        btnOk:setBright(true);
    end
end

-- 是否已经学习了
function UIBishopEquip:isLearn()
    return ME.user.bishop:ownSkill(self.skillId);
end

-- 重绘
function UIBishopEquip:redraw()
    -- 物品
    local itemId = BishopM.getCostItem(self.skillId);

    -- 名称
    local nameLabel = findChildByName(self.node, "CT/name");
    local itemName = ItemM.query(itemId, "name");
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    nameLabel:setString(itemName);

    local descLabel = findChildByName(self.node, "CT/desc");
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    local desc = "";

    local grid = DungeonM.getGridByPos(self.pos);

    -- 描述
    local color = "[gray]";
    if self:isLearn() then
        color = "[blue]";
    end

    -- 描述/详细描述
    desc = ItemM.query(itemId, "desc") .. "\n\n";
    desc = desc .. "[red]" .. ItemM.query(itemId, "detail") .. "[-]" .. "\n\n";

    -- 施法方式
    local castType = SkillM.query(self.skillId, "type");
    if castType == USER_SKILL then
        -- 主动
        local mp = BishopM.calcSkillCost(self.skillId);
        local castDesc;
        if self:isLearn() then
            castDesc = string.format(getLocStr("bine_skill_initiative_cast"), mp);
        else
            castDesc = string.format(getLocStr("bine_skill_initiative_cast2"), mp);
        end
        desc = desc .. castDesc .. "\n";
    end

    -- 技能描述
    if BishopM.querySkill(self.skillId, "active") == 1 then
        skillDesc = SkillM.getSkillDesc(self.skillId, nil, nil, "bishop_active");
    else
        skillDesc = SkillM.getSkillDesc(self.skillId);
    end
    local skillDesc = color .. extractString(skillDesc) .. "[-]";
    desc = desc .. skillDesc;

    -- 按钮显示"装备"/"升级"
    if self:isLearn() then
        btnOk:setTitleText(getLocStr("btn_text_upgrade"));
    else
        btnOk:setTitleText(getLocStr("btn_text_equip"));
    end

    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, desc);

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL, { nameLabel, descLabel, btnOk});
end

-- 尝试装备
function UIBishopEquip:tryLevelUp()
    local itemId = BishopM.getCostItem(self.skillId);

    if ItemM.getAmount(ME.user, itemId) <= 0 then
		-- 消耗不足
		local name = ItemM.query(itemId, "name");

		alert(string.format(getLocStr("item_not_enough"), name));
        return;
    end

    -- 升级
    local ret = false;
    if self:isLearn() then
        if DungeonActionM.go("bishop_skill_upgrade", self.pos, self.skillId) then
            ret = true;
        end
    else
        if DungeonActionM.go("bishop_learn_skill", self.pos, self.skillId) then
            ret = true;
        end
	end

    if ret then
        local uiForm = UIMgr.getCurrentScene():getFormByName("UIBishop");
        uiForm:onSubmit(self.index);
        -- 关闭界面
        UIMgr.getCurrentScene():removeFormByName("UIBishopEquip");
    end
end

